package com.hollis公众号.防止序列化破坏单例模式;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class SerializableDemo2 {

	public static void main(String[] args) throws Exception {
		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("tempfile"));
		oos.writeObject(Singleton2.getSingleton());
		File file = new File("tempfile");
		ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
		Singleton2 newInstance = (Singleton2)ois.readObject();
		//是否是同一个对象
		System.out.println(newInstance == Singleton2.getSingleton());
	}

}
